package com.zhengke.controller.directory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zhengke.common.BaseController;
import com.zhengke.common.object.AjaxDone;
import com.zhengke.common.object.ZTreeNode;
import com.zhengke.dao.SysDictionaryDao;
import com.zhengke.pojo.SysDictionary;
import com.zhengke.pojo.SysDictionaryItem;
import com.zhengke.service.directory.SysDictionaryService;

@Controller
@RequestMapping("directory")
public class DirectoryController extends BaseController {
	@Autowired
	private SysDictionaryService service;
	@Autowired
	SysDictionaryDao dao;
   /**
          * 进入这个功能的controller
    * 
    */
	@RequestMapping("toDirectory.do")
	public String toDirectory() {
		return "directory/directoryMgr";
	}
	
	/**
	 * 生成字典树
	 * @param id
	 * @return
	 */
	
	@ResponseBody
	@RequestMapping("tree.do")
	public List<ZTreeNode> tree(String id){
		List<ZTreeNode> list = new ArrayList<ZTreeNode>();
		if(id!=null) {
			list = service.querySlaveNodes(id);
			return list;
		}
		list = service.queryDic();
		return list;
	}
	/**
	 * 添加根分类
	 * @return
	 */
	@ResponseBody
	@RequestMapping("addParent.do")
	public AjaxDone addParentClass() {
		SysDictionary dic = new SysDictionary();
		dic.setDicName("新增分类");
		dic.setCreateTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
		dic.setCreaterId(1);
		dic.setDicId(dao.getNewDicId());
		try{
			int rst = service.insert(dic);
			if(rst>0)
				return new AjaxDone("200","添加父级分类成功!","",dic);
		}catch(Exception e){
			e.printStackTrace();
		}
		return new AjaxDone("300","系统繁忙,请稍后重试!");
	}
	/**
	 * 添加子集分类
	 */
	@ResponseBody
	@RequestMapping("addSubClass.do")
	public AjaxDone addSubClass(String dicId) {
		SysDictionaryItem dicItem= new SysDictionaryItem();
		dicItem.setDicId(dicId);
		String itemId = dao.getNewDicItemId(dicItem);
		dicItem.setParamName("新增下级分类");
		dicItem.setParamValue("请自己手动修改！");
		dicItem.setItemId(itemId);
		dicItem.setCreateTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
		dicItem.setStatus((short)0);
		//创建者ID
		dicItem.setCreaterId(1);
		try{
		int num = service.insertSub(dicItem);
		if(num>0) 
			return new AjaxDone("200","增加成功!","",dicItem);
		}catch(Exception e){
			e.printStackTrace();
		}
		return new AjaxDone("300","系统繁忙,请稍后重试!");
	}
	
	/**
	 * 到修改页面
	 */
	@RequestMapping("toDicUpd.do")
	public String toUpdGoodsClass(String id,HttpServletRequest request ,ModelMap model) {
		String level = request.getParameter("treeNodeLevel");
		//判断是子节点还是父节点
		//如果是父节点搜索父节点中的数值
		if(Integer.valueOf(level)==0) {
			SysDictionary dic=dao.queryDicById(id);
			model.addAttribute("dic", dic);
			return "directory/toDicUpd";
		}else {
			SysDictionaryItem dicItem=dao.queryDicSubById(id);
			//根据id查询父级name
			SysDictionary dic =  dao.queryDicNameById(id);
			model.addAttribute("dicItem", dicItem);
			model.put("dic", dic);
			return "directory/toDicSubUpd";
		}
	}
	/**
	 * 删除子类
	 * @return
	 */
	@ResponseBody
	@RequestMapping("deleteClass.do")
	public AjaxDone deleteDicItemClass(String itemId) {
		try {
			int rst = service.delDicItemByItemId(itemId);
			if(rst==-1) {
				return new AjaxDone("300","删除失败！");
			}
			if(rst>=0) {
				return new AjaxDone("200","删除成功!");
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		return  new AjaxDone("300","系统繁忙,请稍后重试!");
	}
	/**
	 * 删除父类
	 * @return
	 */
	@ResponseBody
	@RequestMapping("deleteDicClass.do")
	public AjaxDone deleteDicClass(String dicId) {
		
		try {
			//删除父类时判断是否存在子类
			List<SysDictionaryItem> dic =dao.querySlaveNodes(dicId);
		
			if(dic.size()>0) {
				return new AjaxDone("300","删除失败！请先删除子类");
			}
			int rst = service.delDicBydicId(dicId);
			if(rst==-1) {
				return new AjaxDone("300","删除失败!");
			}
			if(rst>=0) {
				return new AjaxDone("200","删除成功!");
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		return  new AjaxDone("300","系统繁忙,请稍后重试!");
	}
	/**
	 * 更新子类信息
	 * @param paramName
	 * @param dicName
	 * @param dicId
	 * @param status
	 * @param paramValue
	 * @param itemId
	 * @return
	 */
	@ResponseBody
	@RequestMapping("updateDicSubClass.do")
	public AjaxDone updateDicSubClass(String paramName,String dicName,String dicId,String status,String paramValue,String itemId) {
		SysDictionaryItem dicItem=new SysDictionaryItem();
		dicItem.setParamName(paramName);
		dicItem.setParamValue(paramValue);
		dicItem.setItemId(itemId);
		dicItem.setStatus(Short.parseShort(status));
		int num =service.updateDicSubClass(dicItem);
		if(num>0) {
			return new AjaxDone("200","保存成功！");
		}
		return new AjaxDone("300","系统繁忙请稍后再试！");
	}
	/**
	 * 更新父类信息
	 */
	@ResponseBody
	@RequestMapping("updateDicClass.do")
	public AjaxDone updateDicClass(String dicName,String dicId) {
		SysDictionary dic =new SysDictionary();
		dic.setDicName(dicName);
		dic.setDicId(dicId);
		int num =service.updateDicClass(dic);
		if(num>0) {
			return new AjaxDone("200","保存成功！");
		}
		return new AjaxDone("300","系统繁忙请稍后再试！");
	}
}
